<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { guild_roomApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading } from '@/utils/public';
  import { getHMMSS } from '@/utils'
  import { ElMessageBox } from "element-plus";
  import Pagination from "@/components/Pagination/index.vue";
  import pushForm from './components/pushForm.vue';
  const pushFormRef = ref()
  const action = reactive({
    total:0,
    list:[] as any[],
  })
  const listQuery = reactive({
    page:1,
    limit:20,
    room_id:'',
  })
  const handleSerach = ()=>{
    listQuery.page = 1
    getList()
  }
  const resetQuery = ()=>{
    Object.assign(listQuery,{
      page:1,
      limit:20,
      room_id:'',
    })
    getList()
  }
  const handlePushForm = (row:any={})=>{
    pushFormRef.value.openDialog(row)
  }
  const handleDel = (row:any={})=>{
    const { room_name, room_id } = row
    ElMessageBox.confirm(`确定移除${room_name}房间推荐吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(() => {
      const loading = Toastloading();
      guild_roomApi.topRoomDel({
        room_id
      }).then(()=>{
        ToastSuccess("操作成功!");
        loading.close();
        getList();
      }).catch(()=>{
        loading.close();
      })
    }).catch(() => {});
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await guild_roomApi.topRoomList(listQuery)
    loading.close()
    const { data=[], total=0 } = res.data
    action.total = total
    action.list = data
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">房间ID：</div>
          <el-input
            v-model="listQuery.room_id"
            placeholder="请输入房间ID"
          />
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" @click="resetQuery">重置</el-button>
          <el-button type="primary" @click="handlePushForm">新增</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border>
      <el-table-column width="140" prop="room_id" label="房间ID" />
      <el-table-column width="140" prop="room_uid" label="房间UID" />
      <el-table-column min-width="140" show-overflow-tooltip prop="room_name" label="房间名称" />
      <el-table-column width="180" prop="sort_num" label="顺序" />
      <el-table-column width="180" prop="open_status" label="房间是否开启">
        <template #default="scope">
          {{ ['未开启','已开启'][scope.row.open_status] }}
        </template>
      </el-table-column>
      <el-table-column width="200" prop="time_st" label="开始时间">
        <template #default="scope">
          {{ getHMMSS(scope.row.time_st * 1000) }}
        </template>
      </el-table-column>
      <el-table-column width="200" prop="time_ed" label="结束时间">
        <template #default="scope">
          {{ getHMMSS(scope.row.time_ed * 1000) }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" width="260" label="操作">
        <template #default="scope">
          <el-button type="primary" @click="handlePushForm(scope.row)">编辑</el-button>
          <el-button type="danger" @click="handleDel(scope.row)" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <pushForm ref="pushFormRef" @update-list="getList" />
  </div>
</template>

<style lang='scss' scoped>

</style>